const mysql = require('./backend/node_modules/mysql2/promise');

async function checkDatabase() {
  const connection = await mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'jindong_mall'
  });

  try {
    console.log('=== 检查 jd_users 表结构 ===');
    const [userColumns] = await connection.execute('DESCRIBE jd_users');
    console.log('用户表字段:');
    userColumns.forEach(col => {
      console.log(`  - ${col.Field} (${col.Type})`);
    });

    console.log('\n=== 检查 jd_orders 表结构 ===');
    const [orderColumns] = await connection.execute('DESCRIBE jd_orders');
    console.log('订单表字段:');
    orderColumns.forEach(col => {
      console.log(`  - ${col.Field} (${col.Type})`);
    });

    console.log('\n=== 测试用户查询 ===');
    const [users] = await connection.execute(`
      SELECT 
        id, username, email, phone, avatar,
        CASE WHEN is_admin = 1 THEN 'admin' ELSE 'user' END as role,
        created_at
      FROM jd_users
      LIMIT 3
    `);
    console.log(`查询到 ${users.length} 个用户`);
    if (users.length > 0) {
      console.log('示例用户:', users[0]);
    }

    console.log('\n=== 测试订单查询 ===');
    const [orders] = await connection.execute(`
      SELECT 
        o.id, o.order_no, o.user_id, o.total_amount, o.actual_amount,
        o.status, o.address, o.created_at, o.updated_at,
        u.username
      FROM jd_orders o
      LEFT JOIN jd_users u ON o.user_id = u.id
      LIMIT 3
    `);
    console.log(`查询到 ${orders.length} 个订单`);
    if (orders.length > 0) {
      console.log('示例订单:', orders[0]);
    }

  } catch (error) {
    console.error('错误:', error.message);
    console.error('详细信息:', error);
  } finally {
    await connection.end();
  }
}

checkDatabase();

